import localforage from 'localforage'

const copyToClipboard = (text) => {
    const textArea = document.createElement("textarea")
    textArea.value = text
    document.body.appendChild(textArea)
    textArea.select();
    document.execCommand("copy")
    document.body.removeChild(textArea)

}

const saveLocalData = (key, value, callback=null) => {
    localforage.setItem(key, value).then(res => {
        if (callback) {
            callback(res)
        }
    })
}

const getLocalData = (key, callback = null) => {
    localforage.getItem(key).then(res => {
        if (callback) {
            callback(res)
        }
    })
}

const removeLocalData = (key, callback = null) => {
    localforage.removeItem(key).then(res => {
        if (callback) {
            callback(res)
        }
    })
}

const hexColor2Rgb = (hex)=> {  
    // 首先去除'#'字符（如果存在）  
    hex = hex.replace('#', '');  
    
    // 检查颜色代码的长度，如果是3位短格式，则转换为6位长格式  
    if (hex.length === 3) {  
      hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];  
    }  
    
    // 分割颜色代码为R, G, B三个部分  
    const r = parseInt(hex.substring(0, 2), 16);  
    const g = parseInt(hex.substring(2, 4), 16);  
    const b = parseInt(hex.substring(4, 6), 16);  
    return r+","+g+","+b;  
  }  
    
export {
    copyToClipboard,
    getLocalData,
    saveLocalData,
    removeLocalData,hexColor2Rgb

}